package com.guodi.model.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.minio.util.MinioUtil;
import com.guodi.core.secure.utils.AuthUtil;
import com.guodi.model.entity.UeProjectAttachment;
import com.guodi.model.entity.UeUpdateRecords;
import com.guodi.model.mapper.UeProjectAttachmentMapper;
import com.guodi.model.service.IUeProjectAttachmentService;
import com.guodi.model.service.IUeUpdateRecordsService;
import com.guodi.model.vo.UeProjectAttachmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @描述：项目管理-附件材料表 服务类
 * @作者: 徐金洲
 * @日期: 2023-05-06
 */
@Service
public class UeProjectAttachmentServiceImpl extends ServiceImpl<UeProjectAttachmentMapper, UeProjectAttachment> implements IUeProjectAttachmentService {

    @Resource
    public IUeProjectAttachmentService ueProjectAttachmentService;

    @Resource
    public IUeUpdateRecordsService ueUpdateRecordsService;

    @Resource
    private UeProjectAttachmentMapper ueProjectAttachmentMapper;


    @Override
    public List<UeProjectAttachment> listTreeByParentId(String projectId) {
        List<UeProjectAttachment> list = ueProjectAttachmentMapper.listTreeByParentId(projectId);
        return list;
    }


    @Override
    public void uploadFile(String projectId, String parentId, MultipartFile[] files) {
        List<UeProjectAttachment> fileList = new ArrayList<>();
        List<String> tempSavePathList = new ArrayList<>();
        try {
            for (MultipartFile multipartFile : files) {
                String filename = multipartFile.getOriginalFilename();
                UeProjectAttachment file = new UeProjectAttachment();
                String url = MinioUtil.uploadFile(multipartFile.getInputStream(), "项目管理附件材料/" + projectId, filename);
                tempSavePathList.add(url);
                //保存附件材料信息
                file.setId(UuidUtil.get32UUID());
                file.setFileName(filename);
                file.setFilePath(url);
                file.setParentId(parentId);
                file.setProjectId(projectId);
                file.setCreator(AuthUtil.getUserId().toString());
                file.setCreateTime(new Date());
                fileList.add(file);
                ueProjectAttachmentService.save(file);
                //保存更新记录数据
                UeUpdateRecords ueUpdateRecords = new UeUpdateRecords();
                ueUpdateRecords.setId(UuidUtil.get32UUID());
                ueUpdateRecords.setProjectId(projectId);
                ueUpdateRecords.setProcessedBy(AuthUtil.getUserId().toString());
                ueUpdateRecords.setUpdateContent("上传材料：" + filename);
                ueUpdateRecords.setUpdateTime(new Date());
                ueUpdateRecordsService.save(ueUpdateRecords);
            }
        } catch (IOException e) {
            MinioUtil.batchRemoveFile(tempSavePathList);
            e.printStackTrace();
        }
    }


    @Override
    public boolean deleteById(String id) {
        UeProjectAttachment file = this.getById(id);
        if(this.removeById(id)) {
            MinioUtil.removeFile(file.getFilePath());
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteByDirId(String id) {
        return ueProjectAttachmentMapper.deleteByDirId(id);
    }

    @Override
    public boolean deleteByProjectId(String ProjectId) {
        return ueProjectAttachmentMapper.deleteByProjectId(ProjectId);
    }


//    @Override
//    public List<UeProjectAttachment> queryFile(String projectId, String fileName) {
//        return ueProjectAttachmentService.queryTree(projectId, fileName);
//    }


//    @Override
//    public List<UeProjectAttachmentVo> queryTree(String projectId, String fileName) {
//        List<UeProjectAttachment> list = this.getBaseMapper().queryTree(tableName, dataId, name);
//        List<UeProjectAttachmentVo> listVo = toVo(list);
//        List<UeProjectAttachmentVo> tree = new ArrayList<>();
//        for (UeProjectAttachmentVo vo : listVo) {
//            if(vo.getParentId().equals("-1")) {
//                List<UeProjectAttachmentVo> child = new ArrayList<>();
//                for (UeProjectAttachmentVo vo2 : listVo) {
//                    if(vo2.getParentId().equals(vo.getId())) {
//                        child.add(vo2);
//                    }
//                }
//                vo.setChildren(child);
//                tree.add(vo);
//            }
//        }
//        return tree;
//    }


    private List<UeProjectAttachmentVo> toVo(List<UeProjectAttachment> list) {
        List<UeProjectAttachmentVo> listVo = new ArrayList<>();
        for (UeProjectAttachment file : list) {
            UeProjectAttachmentVo vo = new UeProjectAttachmentVo();
            BeanUtils.copyProperties(file, vo);
            listVo.add(vo);
        }
        return listVo;
    }
}
